﻿using System;
using System.ComponentModel;
using System.Windows.Markup;
using System.Windows.Media;

namespace ICodeShare.UI.Converters.Markup
{
    #region MarkupExtension
    /// <summary>
    /// Boolean值转换成Color值
    /// </summary>
    /// <example>
    /// 1.在xmal文件引用DateConverter类所在命名空间。
    ///    xmlns:cvts="http://schemas.extended.wpf.com/converters"
    /// 2.在xaml文件中指定Binding值的Converter
    ///    <TextBox Color="{Binding ShowTheTextBox, Converter={cvts:BooleanToColorConverter}}"/>
    ///    <TextBox Color="{Binding ShowTheTextBox, Converter={cvts:BooleanToColorConverter UseHidden=true}}"/>
    ///    <TextBox Color="{Binding ShowTheTextBox, Converter={cvts:BooleanToColorConverter IsInversed=true}}"/>
    ///    <TextBox Color="{Binding ShowTheTextBox, Converter={cvts:BooleanToColorConverter UseHidden=true,IsInversed=true}}"/>
    /// </example>
    [MarkupExtensionReturnType(typeof(BooleanToColorConverter))]
    public sealed class BooleanToColorConverterExtension : MarkupExtension
    {
        #region Members
        private bool _isInversed = false;
        /// <summary>
        /// 条件为真时，颜色值#FFFFFFFF
        /// </summary>
        private Color _trueColor = Colors.White;
        /// <summary>
        /// 条件为假时，颜色值#FF000000
        /// </summary>
        private Color _falseColor = Colors.Black;
        #endregion

        #region Construction
        public BooleanToColorConverterExtension()
        {
        }

        /// <summary>
        /// 初始化一个BooleanToColorConverterExtension类
        /// </summary>
        /// <param name="isInversed">值是否相反</param>
        /// <param name="trueColor">条件为真的颜色</param>
        /// <param name="falseColor">条件为假的颜色</param>
        public BooleanToColorConverterExtension(bool isInversed, Color trueColor, Color falseColor)
        {
            this._isInversed = isInversed;
            this.TrueColor = trueColor;
            this.FalseColor = falseColor;
        }
        #endregion

        #region Properties
        /// <summary>
        /// 取相反值
        /// </summary>
        [ConstructorArgument("isInversed"), DefaultValue(false)]
        public bool IsInversed
        {
            get { return this._isInversed; }
            set { this._isInversed = value; }
        }

        /// <summary>
        /// 条件为真时，颜色值
        /// </summary>
         [ConstructorArgument("trueColor")]
        public Color TrueColor
        {
            get { return this._trueColor; }
            set { this._trueColor = value; }
        }

        /// <summary>
        /// 条件为假时，颜色值
        /// </summary>
         [ConstructorArgument("falseColor")]
        public Color FalseColor
        {
            get { return this._falseColor; }
            set { this._falseColor = value; }
        }
        #endregion

        #region Base Class Overrides
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return new BooleanToColorConverter(this._isInversed, this._trueColor, this._falseColor);
        }
        #endregion
    }
    #endregion
}
